<template>
  <div>
    <div style="margin-top: 10px; margin-left: 10px">
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="关键字">
          <el-input
            v-model="searchObj.keyword"
            placeholder="姓名/手机号"
          ></el-input>
        </el-form-item>
        <el-form-item label="还款方式">
          <el-select v-model="searchObj.returnMethod" placeholder="请选择">
            <el-option
              :label="item.name"
              :value="item.value"
              :key="item.id"
              v-for="item in returnMethodList"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="资金用途">
          <el-select v-model="searchObj.moneyUse" placeholder="请选择">
            <el-option
              :label="item.name"
              :value="item.value"
              :key="item.id"
              v-for="item in moneyUseList"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="借款金额">
          <el-form-item>
            <el-input
              v-model="searchObj.amountStart"
              placeholder="起始"
              type="number"></el-input>
          </el-form-item>
          -
          <el-form-item>
            <el-input
              v-model="searchObj.amountEnd"
              placeholder="截止"
              type="number"
            ></el-input>
          </el-form-item>
        </el-form-item>

        <el-form-item label="借款期限(月)">
          <el-form-item>
            <el-input
              v-model="searchObj.periodStart"
              placeholder="起始"
              type="number"></el-input>
          </el-form-item>
          -
          <el-form-item>
            <el-input
              v-model="searchObj.periodEnd"
              placeholder="截止"
              type="number"
            ></el-input>
          </el-form-item>
        </el-form-item>

        <el-form-item label="创建时间">
          <el-date-picker
            type="date"
            placeholder="开始"
            value-format="yyyy-MM-dd"
            v-model="searchObj.createTimeStart"></el-date-picker>
          -
          <el-date-picker
            type="date"
            placeholder="截止"
            value-format="yyyy-MM-dd"
            v-model="searchObj.createTimeEnd"
          ></el-date-picker>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleQuery">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="info" @click="handleClear">清空</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table :data="borrows" stripe style="width: 100%">
      <el-table-column type="index" label="序号">
        <template slot-scope="scope">
          {{ (pageNum - 1) * pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="name" label="借款人姓名"></el-table-column>
      <el-table-column prop="mobile" label="手机"></el-table-column>
      <el-table-column prop="amount" label="借款金额"></el-table-column>
      <el-table-column prop="period" label="期数"></el-table-column>
      <el-table-column prop="borrowYearRate" label="年化率"></el-table-column>
      <el-table-column prop="returnMethod" label="还款方式"></el-table-column>
      <el-table-column prop="moneyUse" label="资金用途"></el-table-column>
      <el-table-column prop="status" label="状态"></el-table-column>
      <el-table-column prop="createTime" label="申请时间" width="160"></el-table-column>
      <el-table-column label="操作" width="140">
        <template slot-scope="scope">
          <el-row>
            <el-col :span="12">
              <router-link :to="`/borrower/borrow/info/${scope.row.id}`">
                <el-button type="primary" size="mini">查看</el-button>
              </router-link>
            </el-col>
            <el-col :span="12">
              <el-button
                size="mini"
                type="warning"
                @click="handleApproval(scope.row)"
                v-if="scope.row.status === '审核中'">
                审批
              </el-button>
            </el-col>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[1, 2, 3, 4]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

    <el-dialog title="审批" :visible.sync="dialogFormVisible">
      <el-form :model="approval">
        <el-form-item label="是否通过" :label-width="formLabelWidth">
          <el-radio v-model="approval.status" :label="2">通过</el-radio>
          <el-radio v-model="approval.status" :label="-1">不通过</el-radio>
        </el-form-item>

        <el-form-item
          v-if="approval.status == 2"
          label="标的名称"
          :label-width="formLabelWidth">
          <el-input v-model="approval.title"></el-input>
        </el-form-item>
        <el-form-item
          label="起息日"
          v-if="approval.status == 2"
          :label-width="formLabelWidth">
          <el-date-picker
            v-model="approval.lendStartDate"
            type="date"
            placeholder="选择日期"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item
          v-if="approval.status == 2"
          label="年化率"
          :label-width="formLabelWidth">
          <el-input v-model="approval.lendYearRate">
            <template slot="append">%</template>
          </el-input>
        </el-form-item>
        <el-form-item
          v-if="approval.status == 2"
          label="服务费率"
          :label-width="formLabelWidth">
          <el-input v-model="approval.serviceRate">
            <template slot="append">%</template>
          </el-input>
        </el-form-item>
        <el-form-item
          v-if="approval.status == 2"
          label="标的描述"
          :label-width="formLabelWidth">
          <el-input v-model="approval.lendInfo" type="textarea"></el-input>
        </el-form-item>
        <el-form-item label="审批备注" :label-width="formLabelWidth">
          <el-input v-model="approval.content" type="textarea"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleCommit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import borrowerApi from "@/api/borrower";
import dictApi from "@/api/dict";

export default {
  data() {
    return {
      formLabelWidth: "120px",
      approval: {
        status: 2,
        lendYearRate: 0,
        serviceRate: 5,
      }, //接收审批对话框数据
      dialogFormVisible: false,
      //条件对象
      searchObj: {},
      pageNum: 1,
      pageSize: 3,
      total: 0,
      borrows: [],
      //数据字典
      moneyUseList: [],
      returnMethodList: [],
    };
  },
  created() {
    //查询数据字典
    dictApi.listByDictCode("returnMethod").then((r) => {
      this.returnMethodList = r.data.items;
    });
    dictApi.listByDictCode("moneyUse").then((r) => {
      this.moneyUseList = r.data.items;
    });
    this.initData();
  },
  methods: {
    //审批对话框确定按钮事件函数
    handleCommit() {
      borrowerApi.borrowInfoApproval(this.approval).then((r) => {
        this.$message.success("审批成功");
        //关闭对话框
        this.dialogFormVisible = false;
        //清空对话框中的审批对象的缓存
        this.approval = {
          status: 2,
          lendYearRate: 0,
          serviceRate: 5,
        };
        //加载页面中的数据
        this.initData();
      });
    },
    //审批按钮事件函数
    handleApproval(borrowInfo) {
      //设置借款信息部分数据回显到对话框中
      console.log(borrowInfo);
      //borrowInfo代表一行借款信息数据 json对象
      //vue渲染页面时 elementui中修改的属性值如果和默认属性值类型不一致会设置失败
      this.approval.lendYearRate = Number(
        borrowInfo.borrowYearRate.replace("%", "")
      );
      this.approval.id = borrowInfo.id; //后端根据此id进行数据更新
      this.dialogFormVisible = true;
    },
    //清空条件
    handleClear() {
      this.pageNum = 1;
      this.pageSize = 3;
      this.searchObj = {};
      this.initData();
    },
    //条件查询
    handleQuery() {
      console.log(this.searchObj);
      this.initData();
    },
    handleSizeChange(size) {
      this.pageSize = size;
      this.initData();
    },
    handleCurrentChange(num) {
      this.pageNum = num;
      this.initData();
    },
    initData() {
      borrowerApi
        .queryBorrows(this.pageNum, this.pageSize, this.searchObj)
        .then((r) => {
          this.total = Number(r.data.page.total);
          this.borrows = r.data.page.records;
          console.log(r.data);
        });
    },
  },
};
</script>
